package com.nandi.emergencyinspection.ui.activity

import android.graphics.Color
import android.os.Bundle
import android.view.KeyEvent
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.nandi.emergencyinspection.R
import com.nandi.emergencyinspection.mvp.MessageContract
import com.nandi.emergencyinspection.ui.adapter.MessageListAdapter
import com.tbright.ktbaselibrary.extension.isVisiable
import com.tbright.ktbaselibrary.mvp.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_message.*
import kotlinx.android.synthetic.main.fragment_1.view.*
import kotlinx.android.synthetic.main.header_layout.*
import kotlinx.android.synthetic.main.search_layout.*


class MessageActivity : BaseMvpActivity<MessageContract.MessagePresenter>(),
    MessageContract.MessageView {
    private val titles: ArrayList<String> = ArrayList()
    private var listAdapter: MessageListAdapter = MessageListAdapter(titles)
    var page = 0
    private var size = 10
    override fun getLayoutId(): Int {
        return R.layout.activity_message
    }

    override fun initView(savedInstanceState: Bundle?) {
        left_icon.setOnClickListener {
            finish()
        }
        center_txt.text = "消息通知"

        search_edit.setOnEditorActionListener(object : TextView.OnEditorActionListener {
            override fun onEditorAction(p0: TextView?, actionId: Int, p2: KeyEvent?): Boolean {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    search_img.isEnabled = true
                    search_img.setImageResource(R.drawable.icon_close)
                    return true
                }
                return false

            }

        })

        search_img.setOnClickListener {
            search_edit.setText("")
            search_img.isEnabled = false
            search_img.setImageResource(R.drawable.icon_search)
        }
        initAdapter()
    }

    /**
     * 刷新
     */
    private fun initRefreshLayout() {
        swipe.setColorSchemeColors(Color.rgb(47, 223, 189))
        swipe.setOnRefreshListener { // 这里的作用是防止下拉刷新的时候还可以上拉加载
            listAdapter.loadMoreModule.isEnableLoadMore = false
            // 下拉刷新，需要重置页数
            page = 0
            //请求数据

        }
    }

    /**
     * 初始化列表适配器
     */
    private fun initAdapter() {
//        listAdapter.addChildClickViewIds(R.id.text_img, R.id.audio_img)
        listAdapter.setOnItemChildClickListener { adapter, view, position ->
            val any = adapter.data[position] as String
            when (view.id) {
            }
        }
        dataShow.apply {
            layoutManager = LinearLayoutManager(this@MessageActivity)
            adapter = listAdapter
        }
        initLoadMore()
        initRefreshLayout()
        for (data in 0..40) {
            titles.add("item$data")
        }
    }

    /**
     * 初始化上拉加载
     */
    private fun initLoadMore() {
        listAdapter.loadMoreModule.setOnLoadMoreListener {
            swipe.isRefreshing = false
            listAdapter.loadMoreModule.isEnableLoadMore = true
        }
        listAdapter.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        listAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
    }

    /**
     * 设置数据到适配器
     */
    private fun setData(data: MutableList<String>) {
        //省略网络请求数据......
        //网络请求成功后下拉刷新与上拉加载都做如下处理:
        swipe.isRefreshing = false
        listAdapter.loadMoreModule.isEnableLoadMore = true
        if (1 === page) {
            //如果是加载的第一页数据，用 setData()
            listAdapter.setList(data)
        } else {
            //不是第一页，则用add
            listAdapter.addData(data)
        }
        if (data.size < size) {
            //如果不够一页,显示没有更多数据布局
            listAdapter.loadMoreModule.loadMoreEnd()
        } else {
            listAdapter.loadMoreModule.loadMoreComplete()
        }
        // page加一
        ++page
    }

    override fun showResult(result: String) {
        TODO("Not yet implemented")
    }
}